package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.entity.activity.lottery.UserActivityLotteryData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryConfig;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lottery.ActivityLotteryMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_ACTIVITY_LOTTERY_DRAW, desc = "抽奖")
public class LotteryDrawCmd extends AbstractCommand {

    int activityId;
    int drawCount;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityLotteryProto.ActivityLotteryDrawReqMsg reqMsg = ActivityLotteryProto.ActivityLotteryDrawReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        drawCount = reqMsg.getDrawCount();
        if (0 == drawCount) {
            drawCount = 1;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (drawCount != 1 && drawCount != 10) {
            ret = GameErrorCode.E_ERROR_DATA;
            return;
        }
        ActivityLotteryModule lotteryModule = player.getModule(ActivityLotteryModule.class);
        UserActivityLotteryData data = lotteryModule.getUserData(activityId);
        if (data == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        ActivityLotteryConfig config = ActivityLotteryMgr.getActivityLotteryConfig(activityId,data.getLotteryType());
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        if (config.getActivityInfo().getType() == eActivityType.PeerlessBeautyLotteryActivity.getValue()) {
            if (data.getLotteryType() == 0) {
                ret = GameErrorCode.E_ERROR_DATA;
                return;
            }
        }

        long now = System.currentTimeMillis();
        if (now - data.getLastDrawTime() < 2000) {
            ret = GameErrorCode.E_REQUEST_TOO_FAST;
            return;
        }

        Property cost = new Property();
        if (drawCount == 1) {
            cost = PropertyHelper.parseStringToProperty(config.getCost());
        } else {
            cost = PropertyHelper.parseStringToProperty(config.getTenCost());
        }

        // 是否足够道具
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        data.setLastDrawTime(now);

        ret = lotteryModule.drawBox(activityId, drawCount);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_ACTIVITY_LOTTERY_DRAW, ActivityLotteryProto.ActivityLotteryDrawRespMsg.newBuilder().setActivityId(activityId).setRet(ret));
        }
    }
}
